package com.smag.web.controller.farming.mall;

import com.smag.common.core.controller.BaseController;
import com.smag.common.core.domain.AjaxResult;
import com.smag.common.core.domain.entity.FarmCart;
import com.smag.common.utils.SecurityUtils;
import com.smag.system.domain.dto.UpdateShoppingCartDto;
import com.smag.system.domain.vo.ShoppingCartVo;
import com.smag.system.service.FarmCartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *购物车
 */
@RestController
@RequestMapping("/farming/mall/product/cart")
public class CartController extends BaseController {

    @Resource
    private FarmCartService farmCartService;

    /**
     * 新增
     */
    @PostMapping
    public AjaxResult addShoppingCart(@RequestBody FarmCart farmCart)
    {
        farmCart.setUserId(SecurityUtils.getUserId());
        return farmCartService.addShoppingCart(farmCart);
    }


    /**
     * 获取购物车列表
     * @return
     */
    @GetMapping("/getShoppingCart")
    public AjaxResult getShoppingCart(){
        List<ShoppingCartVo> shoppingCarts = farmCartService.getShoppingCart(SecurityUtils.getUserId());
        return success(shoppingCarts);
    }

    @PutMapping("/updateShoppingCart")
    public AjaxResult updateShoppingCart(@RequestBody UpdateShoppingCartDto updateShoppingCartDto){
        updateShoppingCartDto.setUserId(SecurityUtils.getUserId());
        return farmCartService.updateShoppingCart(updateShoppingCartDto);
    }


    @DeleteMapping( "/deleteShoppingCart")
    public AjaxResult deleteShoppingCart(Long productId){

        Long userId = SecurityUtils.getUserId();
        return farmCartService.deleteShoppingCart(userId, productId);
    }




    /*

     //查询所有数据
        @GetMapping
        public Result findAll() {
            return Result.success(cartService.list());
        }


        //新增或者更新
        @PostMapping
        public Result save(@RequestBody Cart cart) {
            Integer userId = TokenUtils.getCurrentUser().getId();
            //相同商品进行处理
            Integer goodsId = cart.getGoodsId();
            QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("goods_id", goodsId);
            queryWrapper.eq("user_id", userId);

            Cart db = cartService.getOne(queryWrapper);
            if (db != null) {
//            db.setNumber(db.getNumber() + cart.getNumber());
                db.setNumber(db.getNumber() + cart.getNumber());
                cartService.updateById(db);
                return Result.success();
            }
            //新增或更新
            if (cart.getId() == null) {
                cart.setUserId(userId);
            }
            cartService.saveOrUpdate(cart);
            return Result.success();
        }

        @PostMapping("/number/{id}/{number}")
        public Result updateNum(@PathVariable Integer id, @PathVariable Integer number) {
            cartMapper.updateNum(number, id);
            return Result.success();
        }

        //删除
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
            return Result.success(cartService.removeById(id));
        }

        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {//批量删除
            return Result.success(cartService.removeByIds(ids));
        }



        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
            return Result.success(cartService.getById(id));
        }

//    @GetMapping("/id/{id}")
//    public Result findOne(@PathVariable String userId) {
//        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("userId", userId);
//        return Result.success(cartService.getOne(queryWrapper));
//    }

        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String name) {
            User currentUser = TokenUtils.getCurrentUser();
            Integer userId = currentUser.getId();
            String role = currentUser.getRole();

            return Result.success(cartMapper.page(new Page<>(pageNum, pageSize), userId, role, name));
        }

    }*/
}
